package servlet;

import anno.Autowired;
import anno.RequestBody;
import anno.ResponseBody;
import dao.MovieDAO;
import pojo.Movie;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/movie")
public class MovieServlet extends BaseServlet {

    @Autowired
    private MovieDAO movieDAO;
    /**
     * 获取所有电影
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult getAllMovies(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        List<Movie> movies = movieDAO.findAll();
        return new ApiResult(200, "获取所有电影成功", movies);
    }
    /**
     * 添加电影
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @param movie 电影对象
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult addMovie(HttpServletRequest req, HttpServletResponse resp, @RequestBody Movie movie) throws SQLException {
        movieDAO.insert(movie);
        return new ApiResult(200, "电影添加成功", null);
    }

    /**
     * 更新电影
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @param movie 电影对象
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult updateMovie(HttpServletRequest req, HttpServletResponse resp, @RequestBody Movie movie) throws SQLException {
        movieDAO.update(movie);
        return new ApiResult(200, "电影更新成功", null);
    }

    /**
     * 删除电影
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @param id   电影ID
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult deleteMovie(HttpServletRequest req, HttpServletResponse resp, String id) throws SQLException {
        movieDAO.deleteById(Integer.parseInt(id));
        return new ApiResult(200, "电影删除成功", null);
    }

    /**
     * 根据ID查询电影
     *
     * @param req  请求对象
     * @param resp 响应对象
     * @param id   电影ID
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult getMovieById(HttpServletRequest req, HttpServletResponse resp, String id) throws SQLException {
        Movie movie = movieDAO.findById(Integer.parseInt(id));
        return new ApiResult(200, "电影查询成功", movie);
    }

    /**
     * 分页模糊查询电影
     *
     * @param req      请求对象
     * @param resp     响应对象
     * @param keyword  模糊查询关键字
     * @param pageNum  当前页码
     * @param pageSize 每页记录数
     * @return ApiResult 响应结果
     * @throws SQLException SQL异常
     */
    @ResponseBody
    public ApiResult findMoviesByPageAndTitle(HttpServletRequest req, HttpServletResponse resp, String keyword, String pageNum, String pageSize) throws SQLException {
        int page = Integer.parseInt(pageNum);
        int size = Integer.parseInt(pageSize);
        Map<String, Object> result = movieDAO.findMoviesByPageAndTitle(keyword, page, size);
        return new ApiResult(200, "分页模糊查询电影成功", result);
    }
}